package com.jemmyee.pizza.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.LogSys;
import com.jemmyee.pizza.service.ILogSysService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.LogBean;


/**
 * @Description:系统日志管理
 * @author:jemmyee@gmail.com
 * @date:2010-10-28
 * @version:v1.0
 */
@Controller
@RequestMapping("/admin/logsys")
public class LogSysController{
	
	private static Log log= LogFactory.getLog(LogSysController.class);
	
	@Autowired
	private ILogSysService<LogSys> logSysService;
	
	
	@RequestMapping(value="/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute LogBean log,HttpServletRequest request){
		  DetachedCriteria dc= DetachedCriteria.forClass(LogSys.class);
		  LogBean bean=new LogBean();
		  if(from.equals("form"))
		   {
			   bean=log;
			   request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, bean);
		   }
		   if(from.equals("page")) 
		   {
			   if(request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN)!=null)
			   {
				   bean=(LogBean)request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			   }
		   }
		   if(from.equals("menu"))
		   {
			   
		   }
		 
		   if(bean.getType()!=null&&bean.getType()!=0){
			  dc.add(Restrictions.eq("type",bean.getType()));
			}
		   //发布时间
			if (bean.getStartTime()!=null&&bean.getEndTime()!= null) {
				dc.add(Restrictions.ge("ltime",bean.getStartTime()));
				dc.add(Restrictions.le("ltime",bean.getEndTime()));
			}
			if (bean.getStartTime()!=null&&bean.getEndTime()== null) {
				dc.add(Restrictions.ge("ltime",bean.getStartTime()));
			}
			if (bean.getStartTime()==null&&bean.getEndTime()!= null) {
				dc.add(Restrictions.le("ltime",bean.getEndTime()));
			}   
		
//		   dc.addOrder(Order.desc("ltime"));
		   ModelAndView mv=new ModelAndView();
		   Page page=logSysService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"ltime");
		   mv.addObject("page",page);
		   mv.addObject("bean",bean);
		   mv.setViewName("/admin/logsys/list");
		   return mv;
	}
	
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Integer id,HttpServletRequest request){
	    logSysService.delete(logSysService.findById(id));
	    return "redirect:/admin/logsys/list/menu/0";
	}
	
	@RequestMapping(value="/delete_batch/{ids}")
	public String deleteBatch(@PathVariable String ids,HttpServletRequest request){
		for(String id:ids.split("_")){
			if(id!=null&&!id.equals("")){
				logSysService.delete(logSysService.findById(Integer.parseInt(id)));
			}
		}
		return "redirect:/admin/logsys/list/menu/0";
	}
	
	
	

}
